<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				修改学生状态
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form-model ref="ruleForm" :model="form" :rules="rules">
					<a-form-model-item label="班级" prop="classID" v-if="!text.ID">
						<a-select :allowClear="true" v-model="form.classID" @search="fetchClass" placeholder="全部"
							optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in ClassList" :key="index" :value="item.ID">
								{{ item.Name }}
							</a-select-option>
						</a-select>
					</a-form-model-item>
					<a-form-item label="年级" v-if="!text.ID">
						<a-date-picker mode="year" placeholder="请选择年级" format="YYYY" :value="gradeYear"
							:open="yearPickShow" @panelChange="handlePanelChange" @openChange="handleOpenChange"
							@change="yearChange" />
					</a-form-item>
					<a-form-model-item label="部门" :prop="UserNoList?'':'DepartCode'" v-if="!text.ID">
						<a-select v-model="form.DepartCode" optionFilterProp="children" showSearch style="width:100%">
							<a-select-option v-for="(item, index) in DepartCodeList" :key="index" :value="item.Code">
								{{ item.Name }}</a-select-option>
						</a-select>
					</a-form-model-item>
					<a-form-model-item label="状态" prop="State">
						<a-radio-group name="radioGroup" v-model="form.State">
							<a-radio :value="1">在校</a-radio>
							<a-radio :value="2">毕业</a-radio>
							<a-radio :value="3">休学</a-radio>
							<a-radio :value="4">退学</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="" class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">
							确认修改</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-model-item>
				</a-form-model>
			</div>
		</div>
	</div>
</template>

<script>
	import moment from 'moment';
	import {
		DoSetManageStudentState,
		GetClassList
	} from '@/api/follow';
	export default {
		props: {
			text: Object,
			DepartCodeList:Array,
			UserNoList:String
		},
		watch: {
			text: function(text) {
				if(text.ID){
					this.form = text;
					this.gradeYear = text.Grade;
				}
			}
		},
		data() {
			return {
				gradeYear: null,
				yearPickShow: false,
				loading: false,
				formLayout: 'horizontal',
				form: this.$form.createForm(this, {
					name: 'coordinated'
				}),
				form: {
					UserNo: '',
					DepartCode: '',
					Grade: '',
					ClassID: '',
					State: ''
				},
				rules: {
					DepartCode: [{
						required: true,
						message: '请选择部门',
						trigger: 'change'
					}],
					ClassID: [{
						required: true,
						message: '请选择班级',
						trigger: 'change'
					}],
					State: [{
						required: true,
						message: '请选择状态',
						trigger: 'change'
					}]
				},
				ClassList:[]
			};
		},
		created() {},
		methods: {
			filterOption(input, option) {
				return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
			},
			async fetchClass(value) {
				this.fetching = true;
				let myReg = /^[^a-zA-Z]+$/;
				if (value && value.length > 0) {
					let data = {};
					data.key = value;
					data.top = 20;
					let res = await GetClassList(data);
					this.fetching = false;
					this.ClassList = res.data.data;
				}
			},
			moment,
			handleOpenChange(status) {
				this.yearPickShow = status;
			},
			handlePanelChange(value, mode) {
				this.gradeYear = moment(value).format('YYYY');
				this.form.Grade = this.gradeYear;
				this.yearPickShow = false;
			},
			yearChange(date, dateString) {
				if (!date) {
					this.gradeYear = null;
					this.form.Grade = this.gradeYear;
				}
			},
			closeFunction(data) {
				this.$emit('closeFun', data);
				this.form = {
					UserNo: '',
					DepartCode: '',
					Grade: '',
					ClassID: '',
					State: ''
				};
				this.gradeYear = null;
				this.fileList = [];
			},
			handleSubmit(e) {
				this.$refs.ruleForm.validate(async valid => {
					if (valid) {
						let data = this.form;
						data.UserNo = this.UserNoList
						let res = await DoSetManageStudentState(data);
						if (res.data.code === 0) {
							this.$message.success(res.data.msg);
							this.closeFunction('1');
						} else {
							this.$message.error(res.data.msg);
						}
					} else {
						return false;
					}
				});
			}
		}
	};
</script>

<style lang="less" scoped>
	/deep/.ant-form-item {
		margin: 20px 50px;
	}

	/deep/.ant-form-item-label {
		width: 100px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}
</style>
